DATABASE = (function(){ return { // gears database
	db: null, dbStatus: false
	 
	,getAll: function(table, sort){ return this.dbTable('SELECT * FROM '+table+(sort?' ORDER BY '+sort:''));	}

	,getByID: function(table,id){ if(!id||!table)return; return this.dbObject('SELECT * FROM '+table+' WHERE id=?',[id]) }

	,countRecords: function(table){ if(!table)return; var ile=this.dbObject('SELECT count(id) as ile FROM '+table); return ile.ile }

	,dbTable: function(sql, vals){
		if (!this.dbStatus) return;
		var records = new Array(), rs = this.db.execute(sql, vals), i, row={};
		if (!rs) return;
		while (rs.isValidRow()){
			i=rs.fieldCount(); row={};
			while(--i>=0) row[rs.fieldName(i)]=rs.field(i)
			records.push(row);
			rs.next(); 
		}
		rs.close();
		return records;
	}
	
	,dbObject: function(sql,vals){
		if (!this.dbStatus) return;
		var rs = this.db.execute(sql, vals);
		if (!rs) return null;
		if (rs.isValidRow()){ 
			var i=rs.fieldCount(), row={};
			while(--i>=0) row[rs.fieldName(i)]=rs.field(i);
		}	
		rs.close();
		return row;
	}
	
	,addData: function (table, rec){
		var fields=new Array(), values= new Array(), vals= new Array();
		for (i in rec){ fields.push(i); values.push('?'); vals.push(rec[i]); }
		if (fields.length) this.dbEdit('insert into '+table+' ('+fields.join()+') values('+values.join()+')', vals) 
		return this.db.lastInsertRowId;
	}

	,editData: function (table, rec, id){
		var fields=new Array(), vals=new Array();
		if (rec.id) delete(rec.id);
		for (i in rec){ fields.push(i+'=?'); vals.push(rec[i]); }
		vals.push(id);
		if (fields.length) return this.dbEdit('update '+table+' set '+fields.join()+' where id=?', vals) 
	}
	
	,delData: function (table, id){ 
		if (!id || !table) return false; 
		return this.dbEdit('delete from '+table+' where id=?', [id]) 
	}
	
	,dbEdit: function(sql, vals){
		if (!this.dbStatus) return;
		this.db.execute(sql, vals);
		return this.db.rowsAffected;
	}
	
	
	,dbInit: function(name){
		if (!window.google || !google.gears){
			msgbox({ type:'error', autoClose:false, text: 'Install <a href="http://gears.google.com/?action=install&name=Tasklog%202&message=enable%20local%20database%20integration&icon_src=http://portable.tasklog.googlecode.com/hg/img/tasklog48.png&return='+location.href+'">Gears</a>' });
			return;
		}
		try {
			this.db = google.gears.factory.create('beta.database');
			if (this.db){
				this.db.open(name);
				this.dbStatus=true; 
			} 
		} 
		catch(e){die(e)}
	}
	
	,resetTable: function(conf){ 
		try{ 
			this.dbEdit('drop table if exists '+conf.tableName);
			this.dbEdit(conf.tableSql); 
			this.dbStatus=true;
		}
		catch(e){ die('DB ERROR: '+e.message)}
	}
	
	,tableInit: function(config){
		if (!this.db || !this.dbStatus) die('table init error: ');
		if (!config.tableName || !config.tableSql) die('config error: ',config);

		var defaults = { tableName:'mytable', tableSql:''  }, conf={};
		$.extend(conf, defaults, config);

		var rs = this.db.execute('select sql from sqlite_master where name=?',[conf.tableName]);
		if (rs){
			if (rs.isValidRow()){
				if (rs.field(0).toLowerCase()===conf.tableSql) this.dbStatus=true; 
				else this.resetTable(conf);
			}
			else this.resetTable(conf);
			rs.close();
		}
		else this.resetTable(conf);
	}	
	
}})();
